package com.nervenets.template.controller.setting.params;

import com.nervenets.general.utils.StringUtils;
import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.template.hibernate.domain.common.Dict;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class DictPagingParams extends EntityPagingParams<Dict> {
    @ApiModelProperty(value = "上级ID")
    private long parentId;
    @ApiModelProperty(value = "是否启用：0全部，1启用，2禁用")
    private int enable;
    @ApiModelProperty(value = "编码")
    private String code;
    @ApiModelProperty(value = "关键词")
    private String keyword;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<Dict> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        addEqualsPredicate(root.get("parentId"), parentId, parentId > 0);
        addYesOrNoPredicate(root.get("enable"), enable, 1, 2);
        addEqualsPredicate(root.get("code"), code, !StringUtils.isBlank(code));
        addOrLikePredicate(keyword, root.get("name"), root.get("remark"), root.get("detail"));
    }
}
